package com.rqian.spring.profile.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.rqian.spring.profile.bean.LoginUser;
import com.rqian.spring.profile.bean.UserProfileBean;
import com.rqian.spring.profile.service.LoginService;

/**
 * 
 * Handle user login request, if login success, redirect to login success page,
 * if failed, redirect to register page.
 * 
 * @author: rqian
 * @version: 1.0, Apr 26, 2012
 */
@Controller
public class LoginController {

	@Autowired
	private LoginService	loginService;



	@RequestMapping(value = "/login.do", method = RequestMethod.POST)
	public ModelAndView checkLogin(@ModelAttribute("loginUser")
	LoginUser user, Model model) {
		boolean isloginSuccess = false;
		isloginSuccess = loginService.checkLoginUserData(user);

		if (isloginSuccess) {
			return new ModelAndView("loginSuccess", "user", user);
		} else {
			model.addAttribute("registUser", new UserProfileBean());
			return new ModelAndView("register");
		}
	}

}
